﻿using UnityEngine;
using System.Collections;

public class shaddcal : MonoBehaviour {
	public Texture otex;
	Renderer red;
	Ray rey;
	RaycastHit hitinfo;
	// Use this for initialization
	void Start () {
		//red = GetComponent<Renderer> ();
		//red.material.shader = Shader.Find ("Decal");
		//Debug.Log (red.material.GetTexture("_DecalTex").name);
		//red.material.SetTextureScale ("_DecalTex",new Vector2(5,5));
		//red.material.SetTextureOffset ("_DecalTex", new Vector2 (-3,-2));
	}
	
	// Update is called once per frame
	void Update () {
	if (Input.GetMouseButton (0)) {
			rey=Camera.main.ScreenPointToRay(Input.mousePosition);
			if(Physics.Raycast(rey,out hitinfo,100.0f)){
				Debug.Log("Hit");
				red=hitinfo.collider.GetComponent<Renderer>();
				//需要decal着色器
				red.material.shader=Shader.Find("Decal");
				//添加decal需要的贴图（弹痕）
				red.material.SetTexture("_DecalTex",otex);
				//设置贴图大小（越大越小）
				red.material.SetTextureScale ("_DecalTex",new Vector2(5,5));
				Debug.Log(hitinfo.textureCoord);
				//获取主贴图UV点（0-1），乘以负的贴图大小，在自身位移0.5以匹配到贴图中心。
				red.material.SetTextureOffset("_DecalTex",hitinfo.textureCoord*-5+new Vector2(0.5f,0.5f));
			}
		}
	}
}
